2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_BUTTONHANDLER_JUCEHEADER__
27 #define __JUCER_BUTTONHANDLER_JUCEHEADER__
29 #include "../../properties/jucer_ComponentChoiceProperty.h"
30 #include "../../properties/jucer_ComponentTextProperty.h"
31 #include "../../properties/jucer_ComponentBooleanProperty.h"
32 #include "jucer_ComponentUndoableAction.h"
35 //==============================================================================
38 class ButtonHandler
: public ComponentTypeHandler
41 //==============================================================================
42 ButtonHandler (const String
& typeDescription_
,
43 const String
& className_
,
44 const std::type_info
& componentClass
,
45 const int defaultWidth_
,
46 const int defaultHeight_
)
47 : ComponentTypeHandler (typeDescription_
, className_
, componentClass
,
48 defaultWidth_
, defaultHeight_
)
51 //==============================================================================
52 void getEditableProperties (Component
* component
, JucerDocument
& document
, Array
<PropertyComponent
*>& properties
)
54 ComponentTypeHandler::getEditableProperties (component
, document
, properties
);
56 Button
* const b
= dynamic_cast <Button
*> (component
);
58 properties
.add (new ButtonTextProperty (b
, document
));
60 properties
.add (new ButtonCallbackProperty (b
, document
));
62 properties
.add (new ButtonRadioGroupProperty (b
, document
));
64 properties
.add (new ButtonConnectedEdgeProperty ("connected left", Button::ConnectedOnLeft
, b
, document
));
65 properties
.add (new ButtonConnectedEdgeProperty ("connected right", Button::ConnectedOnRight
, b
, document
));
66 properties
.add (new ButtonConnectedEdgeProperty ("connected top", Button::ConnectedOnTop
, b
, document
));
67 properties
.add (new ButtonConnectedEdgeProperty ("connected bottom", Button::ConnectedOnBottom
, b
, document
));
70 XmlElement
* createXmlFor (Component
* comp
, const ComponentLayout
* layout
)
72 Button
* const b
= dynamic_cast <Button
*> (comp
);
74 XmlElement
* e
= ComponentTypeHandler::createXmlFor (comp
, layout
);
75 e
->setAttribute ("buttonText", b
->getButtonText());
76 e
->setAttribute ("connectedEdges", b
->getConnectedEdgeFlags());
77 e
->setAttribute ("needsCallback", needsButtonListener (b
));
78 e
->setAttribute ("radioGroupId", b
->getRadioGroupId());
83 bool restoreFromXml (const XmlElement
& xml
, Component
* comp
, const ComponentLayout
* layout
)
85 Button
* const b
= dynamic_cast <Button
*> (comp
);
87 if (! ComponentTypeHandler::restoreFromXml (xml
, comp
, layout
))
90 b
->setButtonText (xml
.getStringAttribute ("buttonText", b
->getButtonText()));
91 b
->setConnectedEdges (xml
.getIntAttribute ("connectedEdges", 0));
92 setNeedsButtonListener (b
, xml
.getBoolAttribute ("needsCallback", true));
93 b
->setRadioGroupId (xml
.getIntAttribute ("radioGroupId", 0));
98 const String
getCreationParameters (Component
* component
)
100 return quotedString (component
->getName());
103 void fillInCreationCode (GeneratedCode
& code
, Component
* component
, const String
& memberVariableName
)
105 ComponentTypeHandler::fillInCreationCode (code
, component
, memberVariableName
);
107 Button
* const b
= dynamic_cast <Button
*> (component
);
109 if (b
->getButtonText() != b
->getName())
112 << memberVariableName
<< "->setButtonText ("
113 << quotedString (b
->getButtonText()) << ");\n";
116 if (b
->getConnectedEdgeFlags() != 0)
120 if (b
->isConnectedOnLeft())
121 flags
.add ("Button::ConnectedOnLeft");
123 if (b
->isConnectedOnRight())
124 flags
.add ("Button::ConnectedOnRight");
126 if (b
->isConnectedOnTop())
127 flags
.add ("Button::ConnectedOnTop");
129 if (b
->isConnectedOnBottom())
130 flags
.add ("Button::ConnectedOnBottom");
133 s
<< memberVariableName
<< "->setConnectedEdges ("
134 << flags
.joinIntoString (" | ") << ");\n";
136 code
.constructorCode
+= s
;
139 if (b
->getRadioGroupId() != 0)
140 code
.constructorCode
<< memberVariableName
<< "->setRadioGroupId ("
141 << b
->getRadioGroupId() << ");\n";
143 if (needsButtonListener (component
))
144 code
.constructorCode
<< memberVariableName
<< "->addListener (this);\n";
147 void fillInGeneratedCode (Component
* component
, GeneratedCode
& code
)
149 ComponentTypeHandler::fillInGeneratedCode (component
, code
);
151 if (needsButtonListener (component
))
153 String
& callback
= code
.getCallbackCode ("public ButtonListener",
155 "buttonClicked (Button* buttonThatWasClicked)",
158 if (callback
.isNotEmpty())
161 const String
memberVariableName (code
.document
->getComponentLayout()->getComponentMemberVariableName (component
));
162 const String
userCodeComment ("UserButtonCode_" + memberVariableName
);
165 << "if (buttonThatWasClicked == " << memberVariableName
166 << ")\n{\n //[" << userCodeComment
<< "] -- add your button handler code here..\n //[/" << userCodeComment
<< "]\n}\n";
170 static bool needsButtonListener (Component
* button
)
172 return button
->getProperties().getWithDefault ("generateListenerCallback", true);
175 static void setNeedsButtonListener (Component
* button
, const bool shouldDoCallback
)
177 button
->getProperties().set ("generateListenerCallback", shouldDoCallback
);
181 //==============================================================================
182 class ButtonTextProperty
: public ComponentTextProperty
<Button
>
185 ButtonTextProperty (Button
* button_
, JucerDocument
& document_
)
186 : ComponentTextProperty
<Button
> ("text", 100, false, button_
, document_
)
190 void setText (const String
& newText
)
192 document
.perform (new ButtonTextChangeAction (component
, *document
.getComponentLayout(), newText
),
193 "Change button text");
196 const String
getText() const
198 return component
->getButtonText();
202 class ButtonTextChangeAction
: public ComponentUndoableAction
<Button
>
205 ButtonTextChangeAction (Button
* const comp
, ComponentLayout
& layout
, const String
& newName_
)
206 : ComponentUndoableAction
<Button
> (comp
, layout
),
209 oldName
= comp
->getButtonText();
215 getComponent()->setButtonText (newName
);
223 getComponent()->setButtonText (oldName
);
228 String newName
, oldName
;
232 class ButtonCallbackProperty
: public ComponentBooleanProperty
<Button
>
235 ButtonCallbackProperty (Button
* button
, JucerDocument
& document
)
236 : ComponentBooleanProperty
<Button
> ("callback", "Generate ButtonListener", "Generate ButtonListener", button
, document
)
240 void setState (bool newState
)
242 document
.perform (new ButtonCallbackChangeAction (component
, *document
.getComponentLayout(), newState
),
243 "Change button callback");
246 bool getState() const { return needsButtonListener (component
); }
249 class ButtonCallbackChangeAction
: public ComponentUndoableAction
<Button
>
252 ButtonCallbackChangeAction (Button
* const comp
, ComponentLayout
& layout
, const bool newState_
)
253 : ComponentUndoableAction
<Button
> (comp
, layout
),
256 oldState
= needsButtonListener (comp
);
262 setNeedsButtonListener (getComponent(), newState
);
270 setNeedsButtonListener (getComponent(), oldState
);
275 bool newState
, oldState
;
279 class ButtonRadioGroupProperty
: public ComponentTextProperty
<Button
>
282 ButtonRadioGroupProperty (Button
* const button_
, JucerDocument
& document_
)
283 : ComponentTextProperty
<Button
> ("radio group", 10, false, button_
, document_
)
287 void setText (const String
& newText
)
289 document
.perform (new ButtonRadioGroupChangeAction (component
, *document
.getComponentLayout(), newText
.getIntValue()),
290 "Change radio group ID");
293 const String
getText() const
295 return String (component
->getRadioGroupId());
299 class ButtonRadioGroupChangeAction
: public ComponentUndoableAction
<Button
>
302 ButtonRadioGroupChangeAction (Button
* const comp
, ComponentLayout
& layout
, const int newId_
)
303 : ComponentUndoableAction
<Button
> (comp
, layout
),
306 oldId
= comp
->getRadioGroupId();
312 getComponent()->setRadioGroupId (newId
);
320 getComponent()->setRadioGroupId (oldId
);
329 class ButtonConnectedEdgeProperty
: public ComponentBooleanProperty
<Button
>
332 ButtonConnectedEdgeProperty (const String
& name
, const int flag_
,
333 Button
* button
, JucerDocument
& document
)
334 : ComponentBooleanProperty
<Button
> (name
, "Connected", "Connected", button
, document
),
339 void setState (bool newState
)
341 document
.perform (new ButtonConnectedChangeAction (component
, *document
.getComponentLayout(), flag
, newState
),
342 "Change button connected edges");
345 bool getState() const
347 return (component
->getConnectedEdgeFlags() & flag
) != 0;
353 class ButtonConnectedChangeAction
: public ComponentUndoableAction
<Button
>
356 ButtonConnectedChangeAction (Button
* const comp
, ComponentLayout
& layout
, const int flag_
, const bool newState_
)
357 : ComponentUndoableAction
<Button
> (comp
, layout
),
361 oldState
= ((comp
->getConnectedEdgeFlags() & flag
) != 0);
369 getComponent()->setConnectedEdges (getComponent()->getConnectedEdgeFlags() | flag
);
371 getComponent()->setConnectedEdges (getComponent()->getConnectedEdgeFlags() & ~flag
);
382 getComponent()->setConnectedEdges (getComponent()->getConnectedEdgeFlags() | flag
);
384 getComponent()->setConnectedEdges (getComponent()->getConnectedEdgeFlags() & ~flag
);
391 bool newState
, oldState
;
397 #endif // __JUCER_BUTTONHANDLER_JUCEHEADER__